package cn.gcBlock.repository;

import cn.gcBlock.po.Node;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface NodeRepository extends JpaRepository<Node, String>, JpaSpecificationExecutor {
    //计算入
    @Query("select sum(coin) from block_node b where  b.toPublicKey= ?1 and( type = 'CREATION' or  type = 'COIN')")
    Long sumGetMoney(String publicKey);
    //计算出
    @Query("select sum(coin) from block_node b where  b.publicKey  = ?1 and( type = 'COIN')")
    Long sumPayMoney(String publicKey);
    //根据类型查询区块是否存在
    long countByPublicKeyAndTypeAndSymbol(String blockAddress, String type,String symbol);
    //根据类型查询区块是否存在
    long countByTypeAndSymbol(String type,String symbol);
    List<Node> findByPublicKeyAndTypeOrderByTimestampDesc(String publicKey, String type);
    //根据符号查找创世链
    Node findByTypeAndSymbol(String type,String symbol);

    @Query("select count(*) from block_node b where  b.publicKey  = ?1 and( type = 'GUARANTEE')")
    int sumGua(String publicKey);

    @Query("select count(*) from block_node b where  b.publicKey  = ?1 and( type = 'CONSIGNOR')")
    int sumCon(String publicKey);

    @Query("select symbol from block_node  where  type = 'CREATION' ")
    List<String> findSymbols();

}
